package
{
	import flash.media.SoundChannel;
	import flash.desktop.NativeApplication;
	import starling.animation.IAnimatable;
	import starling.animation.Transitions;
	import starling.animation.Tween;
	import starling.core.Starling;
	import starling.display.Button;
	import starling.display.Image;
	import starling.events.Event;
	import starling.events.Touch;
	import starling.events.TouchEvent;
	import starling.events.TouchPhase;
	import starling.extensions.PDParticleSystem;
	import starling.extensions.ParticleSystem;
	import starling.textures.Texture;
	import flash.events.EventDispatcher;
	
	public class PlayScreen extends UIScreen
	{		
		private var _playBtn:Button;
		private var _settings:Button;
		private var _channel:SoundChannel;
		
		//private static var HeartpxConfig:XML = XML(new Heartpx());
		//private static var HeartTexture:Texture = Texture.fromBitmap(new Heart());
		
		public function PlayScreen()
		{
			super(); 
			//SoundStore.addEventListener(Event.SOUND_COMPLETE, onComplete);
			var PlayScreenpxConfig:XML = XML(new Assets.PlayScreenpx());
			 var PlayScreenTexture:Texture = Texture.fromBitmap(new Assets.PlayScreen());
			var ps:PDParticleSystem =new PDParticleSystem(PlayScreenpxConfig, PlayScreenTexture);
			ps.x = 160;
			ps.y = 240;
			_playBtn = new Button(Assets.getUIScreens().getTexture("playbtnlarge"));			
			addChild(new Image(Assets.getUIScreens().getTexture("playscreen")));			
			_settings= new Button(Assets.getUIScreens().getTexture("Settings"));
			addChild(ps);
			addChild(_playBtn);
			addChild(_settings);
			
			Starling.juggler.add(ps);
			
			ps.start();
			//ps.start(9999);
			//Assets.SoundStore.stop();
		}
		
		protected override function onAddedToStage(event:Event):void
		{
			super.onAddedToStage(event);
			_settings.y= Starling.current.stage.stageHeight - Starling.current.stage.stageHeight * 0.15;
			_settings.scaleX = 0.5;
			_settings.scaleY = 0.5;
			_settings.visible = false;
			_playBtn.pivotX = _playBtn.width;
			_playBtn.pivotY = _playBtn.height >> 1;
			_playBtn.x = Starling.current.stage.stageWidth >> 1 ;
			_playBtn.y = Starling.current.stage.stageHeight >> 1;
			NativeApplication.nativeApplication.addEventListener(flash.events.Event.DEACTIVATE , handleDeactivate, false, 0, true);
			_playBtn.addEventListener(starling.events.TouchEvent.TOUCH, onTouched);			
		}		
		private function handleDeactivate(event:flash.events.Event):void 
		{
			//the app is now losing focus
			//musicChannel.stop();
			_channel.stop();
		}
		private function onTouched(e:TouchEvent):void
		{						
			var touch:Touch = e.getTouch(stage);			
			
			if(touch.phase == TouchPhase.ENDED)
			{	
				dispatchEvent(new UINavigateEvent(UINavigateEvent.UI_GOTO, true, {screenID:"SelectScreen"}));
			}
		}
		
		public override function onEnter():void
		{
			_channel = Assets.SoundStore.play(0,999);
		}
		
		public override function onExit():void
		{
			_channel.stop();
		}
	}
}